//
//  ViewController.swift
//  WKWebViewTest
//
//  Created by 刘礼燕 on 2017/8/29.
//  Copyright © 2017年 LLyan. All rights reserved.
//

import UIKit
import WebKit


class ViewController: UIViewController,WKNavigationDelegate {

    var _wkwebView:WKWebView?
    
    override func loadView() {
        
        
        _wkwebView = WKWebView()
        _wkwebView?.navigationDelegate = self;
        view = _wkwebView
        
    }
    
//    override var prefersStatusBarHidden: Bool {
//        return true
//    }
    
    
    //重写这个方法ViewController调不到，也就没有作用
//    override var preferredStatusBarStyle: UIStatusBarStyle{
//
//        return .lightContent
//    }
    
    

    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = UIColor.white
        
        //修改UINavigationBar颜色
//        self.navigationController?.navigationBar.barStyle = .blackTranslucent
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blue]
        self.navigationController?.navigationBar.barTintColor = UIColor.green
        
        if let url = URL(string:"https://www.baidu.com") {
            let request = URLRequest(url: url)
            _wkwebView?.load(request as URLRequest)
        }
        
        
        //打印系统所有的字体
        let familyNames = UIFont.familyNames
        for familyName in familyNames {
//            print("##### Family: \(familyName)")
            
            let fontNames = UIFont.fontNames(forFamilyName: familyName)
            for fontName in fontNames {
//                print("**** tFont: \(fontName)")
            }
        }
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.shared.statusBarStyle = .lightContent
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIApplication.shared.statusBarStyle = .default
    }

    
    //MARK:WKWebNavgationDelegate
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
//        print("decidePolicyForNabigationAction.....")
        
        let haderArr = navigationAction.request.allHTTPHeaderFields?.keys
//        print(haderArr ?? "没有数据")
        
        decisionHandler(.allow)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}




//MARK:
//FIXME:继承
class WebView:WKWebView {
    
    required init?(coder: NSCoder) {
        
        if let _view = UIView(coder: coder) {
            super.init(frame: _view.frame, configuration: WKWebViewConfiguration())
            autoresizingMask = _view.autoresizingMask
            //防止iOS7后继承自scrollview的视图自动偏移64个点
            self.translatesAutoresizingMaskIntoConstraints = true
        }else{
             return nil
        }
        
    }
    
    func loadUrl(string:String){
        if let url = URL(string: "https://www.jinshu.com"){
            load(URLRequest(url: url))
        }
    }
    
}

//MARK:
//MARK:自定义WKWebView
class LYWKWebView:WKWebView {
    
    
    override init(frame: CGRect, configuration: WKWebViewConfiguration) {
        
        super.init(frame: frame, configuration: configuration)
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
        
        
    }
    
    override func layoutSubviews() {
        
    }
    
}



//MARK:
//TODO:扩展
extension WKWebView {
    
    func loadTwoUrl(string:String){
        if let url = URL(string: string){
            load(URLRequest(url: url))
        }
    }
    
}







